package com.qiaofang.applet.filter;

import com.alibaba.fastjson.JSON;
import com.qiaofang.applet.common.constant.ResponseCode;
import com.qiaofang.applet.common.context.QfContext;
import com.qiaofang.applet.common.context.QfContextHolder;
import com.qiaofang.applet.common.context.QfContextProvider;
import com.qiaofang.applet.common.dto.DataResponse;
import com.qiaofang.applet.common.util.RedisUtil;
import com.qiaofang.applet.service.other.domain.CityCompany;
import com.qiaofang.applet.service.other.service.CityCompanyService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.stereotype.Component;
import org.springframework.web.filter.GenericFilterBean;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;

/**
 * 请求过滤器
 *
 * @author QF2019
 */
@Component
public class RequestFilter extends GenericFilterBean {

    @Autowired
    private QfContextProvider contextProvider;

    private List<String> excludeList = Arrays.asList("/api/web/yxIm/receiveMsg", "/api/web/yxIm/external/getYxIMToken", "/api/web/yxIm/getYxIMToken", "/api/web/rockOpen/searchProperty");

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        String url = ((HttpServletRequest) servletRequest).getServletPath();
        if(excludeList.contains(url)) {
            filterChain.doFilter(servletRequest, servletResponse);
            return;
        }

        // 1、判断是否已经存在QfContext上下文，如果存在则继续执行后面的逻辑
        if (existQfContext()) {
            filterChain.doFilter(servletRequest, servletResponse);
            return;
        }

        // 2、上下文件不存在，则从header里取出corporationCode，然后初始化QfContext上下文
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        String corporationCode = request.getHeader("corporationCode");

        if (StringUtils.isEmpty(corporationCode)) {
            writeResponse(servletResponse);
            return;
        }

        // 3、corporationCode初始化QfContext上下文
        initQfContext(corporationCode);

        filterChain.doFilter(servletRequest, servletResponse);
    }

    /**
     * 判断QfContext是否存在
     *
     * @return
     */
    private static boolean existQfContext() {
        QfContext qfContext = QfContextHolder.getContext();
        if (qfContext == null) {
            return false;
        }

        return StringUtils.isNotEmpty(qfContext.getCorporationCode());
    }

    /**
     * 据集团编码查询集团授权信息
     *
     * @param corporationCode
     * @return
     */
    private void initQfContext(String corporationCode) {
//        CityCompany cityCompany = cityCompanyService.getDefaultCityCompany(corporationCode);
        QfContext qfContext = new QfContext();
        qfContext.setCorporationCode(corporationCode);
        contextProvider.setContext(qfContext);
    }

    /**
     * 输出响应结果
     *
     * @param response
     * @throws IOException
     */
    private void writeResponse(ServletResponse response) throws IOException {
        DataResponse dataResponse = DataResponse.fail(ResponseCode.AUTH_FAIL.getStatus(), ResponseCode.AUTH_FAIL.getMessage());
        response.getWriter().write(JSON.toJSONString(dataResponse));
    }
}
